<?php
    session_start();
    $error = "false";
    if(!empty($_POST['username'])){
        $loginurl= "http://localhost:8080/TestYourselfASE/rest/login/".$_POST['username']."/".$_POST['password'];
        $loginfile= file_get_contents($loginurl);
        $loginobj = json_decode($loginfile);

        if ( !isset($loginobj->Error)) {
            $_SESSION['loggedin']= $loginobj->UserName;
            $_SESSION['userid']= $loginobj->UserId;
            if ($loginobj->IsAdmin == "true") $_SESSION['isadmin']="true";
            else $_SESSION['isadmin'] = "false";
            header('Location: select.php');

        }
        else $error = "true";
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../assets/ico/favicon.ico">

    <title>TestYourSelf - Login</title>

    <!-- Bootstrap core CSS -->
    <link href="../../bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/signin.css" rel="stylesheet">
    <link href="css/template.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>



<div class="site-wrapper">

    <div class="site-wrapper-inner">

        <div class="cover-container">

            <div class="masthead clearfix">
                <div class="inner">
                    <h3 class="masthead-brand">TestYourSelf</h3>
                    <ul class="nav masthead-nav">
                        <li class="active"><a href="#">Login</a></li>
                        <li><a href="features.html">Features</a></li>
                    </ul>
                </div>
            </div>

            <div class="inner cover">
                <h1 class="cover-heading">Willkommen bei TestYourSelf</h1>

                <?php if(isset($_GET['action']) && $_GET['action']=="logout") { ?>
                    <div class="info">Logout erfolgreich</div>
                <?php } elseif (isset($_GET['action']) && $_GET['action']=="register"){ ?>
                    <div class="info">Registrierung erfolgreich</div>
                <?php } ?>

                <?php if($error=="true") echo ' <div class="loginerror">Benutzername und/oder Passwort falsch!</div>'; ?>

                <form class="form-signin" role="form" action="index.php" method="post">
                    <div class="form-heading">Bitte einloggen:</div>
                    <input type="text" name="username" class="form-control" placeholder="Benutzername" required autofocus>
                    <input type="password" name="password" class="form-control" placeholder="Password" required>
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Einloggen</button>
                </form>
                <a href="register.php" class="registerbtn btn btn-lg btn-primary btn-block">Registrieren</a>
            </div>

            <div class="mastfoot">
                <div class="inner">
                    <p>by Christoph Schuster, Martin Schmied, Thomas Stoellner, Philipp Taeubel</p>
                </div>
            </div>

        </div>

    </div>

</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="../../bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

